﻿package mortal.game.view.copy.escort
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.mui.controls.*;
    import extend.language.*;
    import mortal.common.net.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.escort.vo.*;
    import mortal.game.view.guildNew.mercenary.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.game.view.task.data.*;
    import mortal.game.view.task.drama.*;
    import mortal.game.view.vip.*;
    import mortal.mvc.core.*;

    public class EscortController extends CopyControllerBase
    {
        private var _secTimer:SecTimer;
        private var _entityInfo:EntityInfo;
        private var _hasNotFar:Boolean;

        public function EscortController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.EscortCopyOpenSelectTaskWindow, this.onOpenSelectTaskWindowHandler);
            Dispatcher.addEventListener(EventName.EscortCopyOper, this.onOperHandler);
            Dispatcher.addEventListener(EventName.SceneAddSelfCar, this.onSceneAddSelfCar);
            NetDispatcher.addCmdListener(ServerCommand.EscortCopyEscortInfo, this.onEscortInfo);
            NetDispatcher.addCmdListener(ServerCommand.EscortCopyGetEscortSucceed, this.hideWin);
            NetDispatcher.addCmdListener(ServerCommand.TaskDoingDel, this.getEscortTaskRewardComplete);
            NetDispatcher.addCmdListener(ServerCommand.EscortFirstRobName, this.showRobName);
            Dispatcher.addEventListener(EventName.ActiveStart, this.activesStartHandler);
            Dispatcher.addEventListener(EventName.ActiveEnd, this.activesEndHandler);
            Dispatcher.addEventListener(EventName.ActiveNotic, this.activesNoticHandler);
            return;
        }// end function

        private function onOpenSelectTaskWindowHandler(event:DataEvent) : void
        {
            var _loc_2:EscortCopyOperVo = null;
            if (Cache.instance.role.entityInfo.level < GameDefConfig.instance.getSystemOpenLevel(52))
            {
                return MsgManager.showRollTipsMsg(Language.getStringByParam(35171, GameDefConfig.instance.getSystemOpenLevel(52)), MsgRollTipsType.msgRollTips3);
            }
            if (EscortModule.isViewShow)
            {
                EscortModule.instance.updateBtnVible();
                return;
            }
            if (Cache.instance.copy.escort.sEscortInfo)
            {
                this.onEscortInfo(Cache.instance.copy.escort.sEscortInfo);
            }
            else
            {
                _loc_2 = new EscortCopyOperVo(EEscortOper._EEscortOperEnterGetTask);
                GameProxy.copy.escortOper(_loc_2);
            }
            return;
        }// end function

        private function activesStartHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type == EActiveType._EActiveTypeEscort)
            {
                if (Cache.instance.role.entityInfo.level >= GameDefConfig.instance.getSystemOpenLevel(52))
                {
                    EscortCopyIcon.instance.show();
                    EscortCopyIcon.instance.playEffect();
                    EscortCopyIcon.instance.setLeftTimeAndState((_loc_2.endDt.time - ClockManager.instance.nowDate.time) * 0.001, 2);
                }
                else if (EscortCopyIcon.isViewShow)
                {
                    EscortCopyIcon.instance.hide();
                }
            }
            return;
        }// end function

        private function activesEndHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type == EActiveType._EActiveTypeEscort)
            {
                if (EscortCopyIcon.isViewShow)
                {
                    EscortCopyIcon.instance.hide();
                }
            }
            return;
        }// end function

        private function activesNoticHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type == EActiveType._EActiveTypeEscort)
            {
                if (Cache.instance.role.entityInfo.level >= GameDefConfig.instance.getSystemOpenLevel(52))
                {
                    EscortCopyIcon.instance.show();
                    EscortCopyIcon.instance.setLeftTimeAndState((_loc_2.startDt.time - ClockManager.instance.nowDate.time) * 0.001, 1);
                }
                else if (EscortCopyIcon.isViewShow)
                {
                    EscortCopyIcon.instance.hide();
                }
            }
            return;
        }// end function

        private function hideWin(param1:Object) : void
        {
            EscortModule.instance.hide();
            return;
        }// end function

        private function onOperHandler(event:DataEvent) : void
        {
            var _loc_3:SEscortInfo = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:ShopItemData = null;
            var _loc_2:* = event.data as EscortCopyOperVo;
            if (_loc_2.oper == EEscortOper._EEscortOperRefresh)
            {
                if (CopyUtil.isHasNomalEscortTask)
                {
                    return MsgManager.showRollTipsMsg(Language.getString(35172), MsgRollTipsType.msgRollTips3);
                }
                _loc_3 = Cache.instance.copy.escort.sEscortInfo;
                if (_loc_3.color == EColor._EColorOrange)
                {
                    return MsgManager.showRollTipsMsg(Language.getString(35173), MsgRollTipsType.msgRollTips3);
                }
                _loc_4 = int(VipConfig.instance.getValueByTypeAndLv(EVipRecordType._EVipRecordTypeEscortRefresh, VipUtil.vipLevel));
                _loc_5 = 3 + _loc_4 - _loc_3.freeRefreshNum;
                if (_loc_5 <= 0)
                {
                    _loc_6 = Cache.instance.pack.backPackCache.getItemCountByItemCode(410221001);
                    if (_loc_6 > 0)
                    {
                        GameProxy.copy.escortOper(_loc_2);
                    }
                    else if (_loc_2.autoBuy)
                    {
                        _loc_7 = ShopConfig.instance.getFastBuyItemByCode(410221001);
                        if (Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitCoinBind, _loc_7.totalPrize))
                        {
                            GameProxy.copy.escortOper(_loc_2);
                        }
                        else
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowBindCoinPackWin, _loc_7.totalPrize));
                            MsgManager.showRollTipsMsg(Language.getString(35174), MsgRollTipsType.msgRollTips3);
                        }
                    }
                    else
                    {
                        MsgManager.showRollTipsMsg(Language.getString(35175), MsgRollTipsType.msgRollTips3);
                    }
                }
                else
                {
                    GameProxy.copy.escortOper(_loc_2);
                }
            }
            else
            {
                GameProxy.copy.escortOper(_loc_2);
            }
            return;
        }// end function

        private function onEscortInfo(param1:Object) : void
        {
            if (EscortModule.instance.isHide)
            {
                EscortModule.instance.show();
            }
            if (Cache.instance.copy.escort.isInDoubleEscort == true)
            {
                Cache.instance.copy.escort.isInDoubleEscort = false;
            }
            else
            {
                EscortModule.instance.updateBtnVible();
            }
            EscortModule.instance.updateInfo(param1 as SEscortInfo);
            return;
        }// end function

        private function getEscortTaskRewardComplete(param1:Object) : void
        {
            var _loc_2:* = param1 as TaskInfo;
            if (_loc_2 && _loc_2.stask && _loc_2.stask.group == ETaskGroup._ETaskGroupEscort)
            {
                CallLater.setCallLater(this.showEscortAlert, 0.5, _loc_2);
            }
            return;
        }// end function

        private function showEscortAlert(param1:Array) : void
        {
            var num:int;
            var vipNum:int;
            var arr:* = param1;
            var info:* = arr[0] as TaskInfo;
            if (info.stask.group == ETaskGroup._ETaskGroupEscort)
            {
                Cache.instance.copy.escort.sEscortInfo = null;
                num = Cache.instance.task.getGroupFinishCount(ETaskGroup._ETaskGroupEscort);
                if (num < 3)
                {
                    var onCloseHandler:* = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    if (!TaskDramaOperator.isInFlyDrama)
                    {
                        TaskUtil.linkTask(Cache.instance.task.getTaskInfoByGroup(ETaskGroup._ETaskGroupEscort), true);
                    }
                }
                return;
            }// end function
            ;
                    Alert.mode = Alert.Mode_NoneNotModal;
                    Alert.buttonWidth = 70;
                    Alert.timerOut = 5;
                    Alert.okLabel = Language.getString(35176);
                    Alert.calcelLabel = Language.getString(35177);
                    vipNum = int(VipConfig.instance.getValueByTypeAndLv(EVipRecordType._EVipRecordTypeEscort, VipUtil.vipLevel));
                    Alert.show(Language.getStringByParam(35178, 3 - num + vipNum), null, Alert.OK | Alert.CANCEL, null, onCloseHandler);
                }
                else if (num == 3)
                {
                    var clickNpc:* = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    if (!TaskDramaOperator.isInFlyDrama)
                    {
                        AIManager.gotoOrSendtoNpc(1100008, true);
                    }
                }
                return;
            }// end function
            ;
                    Alert.mode = Alert.Mode_NoneNotModal;
                    Alert.buttonWidth = 70;
                    Alert.timerOut = 5;
                    Alert.okLabel = Language.getString(35176);
                    Alert.calcelLabel = Language.getString(35177);
                    Alert.show(Language.getStringByParam(35179, GameConst.ExtraEscortCost), null, Alert.OK | Alert.CANCEL, null, clickNpc);
                }
                else
                {
                    NetDispatcher.removeCmdListener(ServerCommand.TaskDoingDel, this.getEscortTaskRewardComplete);
                }
            }
            return;
        }// end function

        private function escortTaskUpdate(param1:Object) : void
        {
            Cache.instance.copy.escort.sEscortInfo = null;
            var _loc_2:* = Cache.instance.task.getTaskInfoByGroup(ETaskGroup._ETaskGroupEscort);
            if (_loc_2)
            {
                this.killSecTimer();
                NetDispatcher.removeCmdListener(ServerCommand.TaskUpdate, this.escortTaskUpdate);
            }
            return;
        }// end function

        private function onSceneAddSelfCar(event:DataEvent) : void
        {
            this._entityInfo = event.data as EntityInfo;
            if (this._entityInfo && CopyUtil.isInNomalEscortTask)
            {
                if (!this._secTimer)
                {
                    this._secTimer = new SecTimer(3);
                }
                this._secTimer.reset();
                this._secTimer.start();
                this._secTimer.addListener(TimerType.ENTERFRAME, this.onTimeChange);
                NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.escortTaskUpdate);
                EscortRightInfoWin.instance.show();
                EscortRightInfoWin.instance.updateEscortName(this._entityInfo.entityInfo.name);
                GuildNewMercenaryProcessor.canRentCopy = true;
            }
            return;
        }// end function

        private function onTimeChange(param1:SecTimer) : void
        {
            var _loc_3:Number = NaN;
            var _loc_2:* = ThingUtil.entityUtil.getEntity(this._entityInfo.entityInfo.entityId);
            if (_loc_2)
            {
                _loc_3 = GeomUtil.calcDistance(_loc_2.x2d, _loc_2.y2d, RolePlayer.instance.x2d, RolePlayer.instance.y2d);
            }
            else
            {
                _loc_3 = 1000;
            }
            if (_loc_3 > EscortCopyCache.FAR_PX)
            {
                if (!this._hasNotFar)
                {
                    MsgManager.showTaskTarget(Language.getString(35180), 160);
                    if (!TaskDramaOperator.isInFlyDrama)
                    {
                        AIManager.cancelAll();
                    }
                    EscortSmallIcon.instance.show();
                    this._hasNotFar = true;
                    if (EscortRightInfoWin.isViewShow)
                    {
                        EscortRightInfoWin.instance.updateState(1);
                    }
                }
            }
            else
            {
                if (MsgManager.taskTargetTxt && MsgManager.taskTargetTxt.indexOf(Language.getString(35181)) >= 0)
                {
                    return;
                }
                MsgManager.removeTaskTarget();
                EscortSmallIcon.instance.hide();
                this._hasNotFar = false;
                if (EscortRightInfoWin.isViewShow)
                {
                    EscortRightInfoWin.instance.updateState(0);
                }
            }
            return;
        }// end function

        private function killSecTimer() : void
        {
            if (this._secTimer)
            {
                this._secTimer.stop();
                this._secTimer.dispose(true);
                this._secTimer = null;
            }
            MsgManager.removeTaskTarget();
            EscortSmallIcon.instance.hide();
            this._hasNotFar = false;
            if (EscortRightInfoWin.isViewShow)
            {
                EscortRightInfoWin.instance.hide();
                GuildNewMercenaryProcessor.canRentCopy = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrackExceptBtn, true));
            }
            return;
        }// end function

        private function showRobName(param1:SPublicStr) : void
        {
            if (!TaskDramaOperator.isInFlyDrama)
            {
                AIManager.cancelAll();
            }
            MsgManager.showTaskTarget(param1.publicStr + Language.getString(35181), 5);
            return;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            super.enterCopy(param1);
            if (EscortCopySelectTaskWindow.hasInstance && !EscortCopySelectTaskWindow.instance.isHide)
            {
                EscortCopySelectTaskWindow.instance.hide();
            }
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            cache.copy.escort.hasGetEscortTask = false;
            cache.copy.escort.hasGetHarryTask = false;
            cache.copy.escort.notCanMove = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrack, true));
            if (EscortCopyEscortMiniView.hasInstance && !EscortCopyEscortMiniView.instance.isHide)
            {
                EscortCopyEscortMiniView.instance.hide();
            }
            if (EscortCopyHarryMiniView.hasInstance && !EscortCopyHarryMiniView.instance.isHide)
            {
                EscortCopyHarryMiniView.instance.hide();
            }
            if (EscortCopyEscortBar.hasInstance && !EscortCopyEscortBar.instance.isHide)
            {
                EscortCopyEscortBar.instance.hide();
            }
            if (EscortCopyHarryBar.hasInstance && !EscortCopyHarryBar.instance.isHide)
            {
                EscortCopyHarryBar.instance.hide();
            }
            return;
        }// end function

    }
}
